/*引入axios---全局拦截*/
import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'

axios.defaults.baseURL = "http://localhost:2026"

/*前置拦截*/
axios.interceptors.request.use(config => {

    return config
})

axios.interceptors.response.use(response => {
        let res = response.data;
        if (res.code === 200) {
            return response
        } else {
            Element.Message.error(response.data.msg, { duration: 3 * 1000 })
            return Promise.reject(response.data.msg)
        }
    },
    error => {
        if (error.response.data) {
            error.message = error.response.data.msg
        }
        /*console.log(error)*/
        if (error.response.status === 401) {
            store.commit("REMOVE_INFO")
            router.push("/login")
        } else {
            Element.Message.error(error.message, { duration: 3 * 1000 })
            return Promise.reject(error)
        }
    })